package com.gjx.leetcode;
//86. 分隔链表
//给定一个链表和一个特定值 x，对链表进行分隔，使得所有小于 x 的节点都在大于或等于 x 的节点之前。
//
//你应当保留两个分区中每个节点的初始相对位置。
//
//示例:
//
//输入: head = 1->4->3->2->5->2, x = 3
//输出: 1->2->2->4->3->5

public class Solution086 {
	public ListNode partition(ListNode head, int x) {
        if (head == null) {
        	return null;
        }
        
        ListNode tiny = new ListNode(0);
        ListNode tinyCur = tiny;
        ListNode large = new ListNode(0);
        ListNode largeCur = large;
        ListNode cur = head;
        
        while (cur != null) {
        	if (cur.val < x) {
        		tinyCur.next = cur;
        		tinyCur = tinyCur.next;
        	} else {
        		largeCur.next = cur;
        		largeCur = largeCur.next;
        	}
        	cur = cur.next;
        }
        largeCur.next = null;
        tinyCur.next = large.next;
        return tiny.next;
        
    }
}
